//
//  MBAlterViewController.m
//  MBAlterViewController
//
//  Created by zengcatch on 16/1/13.
//  Copyright © 2016年 catch. All rights reserved.
//

#import "MBAlterViewController.h"

@interface MBAlterViewController ()

@property (strong, nonatomic) UIWindow *keyWindow;
@property (weak, nonatomic) UIWindow *previousWindow;
@property (nonatomic, assign) CGRect oriFrm;

@end

@implementation MBAlterViewController

-(instancetype)init{
    self = [super init];
    if (self) {
        _animationType = MBAlterAnimationScale;
        _isShowing = NO;
    }
    return self;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.backView.backgroundColor = [UIColor colorWithWhite:.1f alpha:.5f];
    _oriFrm = self.mainView.frame;
    [self.view bringSubviewToFront:self.mainView];
}

-(void)showView{
    _isShowing = YES;
    _previousWindow = [UIApplication sharedApplication].keyWindow;
    
    _keyWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _keyWindow.rootViewController = self;
    _keyWindow.windowLevel = UIWindowLevelNormal + 100;
    _keyWindow.backgroundColor = [UIColor clearColor];
    [_keyWindow makeKeyAndVisible];
    
    [self reset];
    
    if(_animationType == MBAlterAnimationScale){
        [self scaleAnimation];
    }else if(_animationType ==  MBAlterAnimationUp){
        [self upAnimation];
    }else if(_animationType == MBAlterAnimationDown){
        [self downAnimation];
    }
}

-(void)reset{
    self.backView.alpha = 1.0f;
    self.mainView.alpha = 1.0f;
    self.mainView.transform = CGAffineTransformMakeScale(1.0, 1.0);
}

-(void)upAnimation{
    CGRect changeFrm = _oriFrm;
    changeFrm.origin.y = [UIScreen mainScreen].bounds.size.height;
    
    self.mainView.alpha = .5f;
    self.mainView.frame = changeFrm;
    
    [UIView animateWithDuration:0.8 // 动画时长
                          delay:0.0 // 动画延迟
         usingSpringWithDamping:0.8 // 类似弹簧振动效果 0~1
          initialSpringVelocity:2.0 // 初始速度
                        options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
                     animations:^{
                         self.mainView.alpha = 1.0f;
                         self.mainView.frame = _oriFrm;
                     } completion:^(BOOL finished) {
                     }];
}

-(void)downAnimation{
    CGRect changeFrm = _oriFrm;
    changeFrm.origin.y = -changeFrm.size.height;
    
    self.mainView.alpha = .5f;
    self.mainView.frame = changeFrm;
    
    [UIView animateWithDuration:0.8 // 动画时长
                          delay:0.0 // 动画延迟
         usingSpringWithDamping:0.8 // 类似弹簧振动效果 0~1
          initialSpringVelocity:2.0 // 初始速度
                        options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
                     animations:^{
                         self.mainView.alpha = 1.0f;
                         self.mainView.frame = _oriFrm;
                     } completion:^(BOOL finished) {
                     }];
}

-(void)scaleAnimation{
    self.mainView.alpha = .5f;
    self.mainView.transform = CGAffineTransformMakeScale(0.05, 0.05);
    [UIView animateWithDuration:0.8 // 动画时长
                          delay:0.0 // 动画延迟
         usingSpringWithDamping:0.8 // 类似弹簧振动效果 0~1
          initialSpringVelocity:2.0 // 初始速度
                        options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
                     animations:^{
                         self.mainView.alpha = 1.0f;
                         self.mainView.transform = CGAffineTransformMakeScale(1.0, 1.0);
                     } completion:^(BOOL finished) {
                     }];
}

-(void)dismissView{
    _isShowing = NO;
    self.backView.alpha = 0.0f;
    self.mainView.alpha = 0.0f;
    self.mainView.transform = CGAffineTransformMakeScale(0.0, 0.0);
    [self performSelector:@selector(dealyMethod) withObject:nil afterDelay:.2f];
}

-(void)dealyMethod{
    [_keyWindow resignKeyWindow];
    [_previousWindow makeKeyAndVisible];
    _keyWindow = nil;
}

@end